package com.itheima.EX;

import org.apache.http.Header;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试模拟机器人登陆
 * (缺陷,现在还不能做到验证码自动输入或者避开)
 */
public class Test_Login {
    public static void main(String[] args) throws Exception {
        //确定地址 这里切记,要再登陆之前的地址,否则直接就是200状态,没有302状态,无法进行其他参数设置
        String loginUrl = "http://home.manmanbuy.com/login.aspx?tourl=http%3a%2f%2fhome.manmanbuy.com%2fusercenter.aspx";
        //创建对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //设置请求方式 , 设置请求头：user-agent  reffer
        HttpPost httpPost = new HttpPost(loginUrl);
        httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36");
        httpPost.setHeader("Referer","http://home.manmanbuy.com/login.aspx?tourl=http%3a%2f%2fhome.manmanbuy.com%2fusercenter.aspx");
        //封装数据请求
        List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();

        //添加页面本身依赖的请求 通过页面源码直接拷贝,必须的配置项
        list.add(new BasicNameValuePair("__VIEWSTATE","/wEPDwULLTIwNjQ3Mzk2NDFkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQlhdXRvTG9naW4voj01ABewCkGpFHsMsZvOn9mEZg=="));
        list.add(new BasicNameValuePair("__EVENTVALIDATION","/wEWBQLW+t7HAwLB2tiHDgLKw6LdBQKWuuO2AgKC3IeGDJ4BlQgowBQGYQvtxzS54yrOdnbC"));
        //添加正常的登陆信息
        list.add(new BasicNameValuePair("txtUser", "itcast"));
        list.add(new BasicNameValuePair("txtPass", "www.itcast.cn"));
        list.add(new BasicNameValuePair("autoLogin", "on"));
        list.add(new BasicNameValuePair("btnLogin", "登陆"));//设置按钮的name和value表示模拟回车
        //把输入数据编码成合适的内容
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
        //post请求传递参数
/*

            传递参数其实是两种,setEntity和setString
            这里选择第二种,因为之前设置了合适当前参数的编码
*/

        httpPost.setEntity(entity);
        //发送请求,获取响应
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //获取返回的状态码 查看网页跳转情况可得是重定向302
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode+"****");

        if (statusCode == 302) {
            /*
                因为跳转的地址在location的信息里,先获取一下
             */
            Header[] locations = response.getHeaders("Location");
            String newUrl = locations[0].getValue();
            //再获取一下set-cookie,让系统认为我们登陆了
            Header[] cookies = response.getHeaders("Set-Cookie");

            //设置发送的方式和地址
            HttpGet httpGet = new HttpGet(newUrl);
            //携带cookie
            httpGet.setHeaders(cookies);
            //发送请求
            CloseableHttpResponse responseLogin = httpClient.execute(httpGet);
            //设置编码集合,获取html页面
            String html = EntityUtils.toString(responseLogin.getEntity(), "UTF-8");
            //获取document对象
            Document document = Jsoup.parse(html);
            //解析页面,获取指定的内容
            Elements elements = document.select("#aspnetForm > div.udivright > div:nth-child(2) > table > tbody > tr > td:nth-child(1) > table:nth-child(2) > tbody > tr > td:nth-child(2) > div:nth-child(1) > font");
            //得到积分的具体值
            String jf = elements.html();
            System.out.println("用户积分是:" + jf);

        }
        //关闭资源
        httpClient.close();


    }
}
